package com.ruoyi.pingan.fymodule.order.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.pingan.fymodule.order.entity.SxoOrderAftersale;
import com.ruoyi.pingan.fymodule.order.entity.SxoSafetyOrder;
import com.ruoyi.pingan.fymodule.order.entity.SxoSafetyOrderRevoke;
import com.ruoyi.pingan.fymodule.order.mapper.SxoSafetyOrderRevokeMapper;
import com.ruoyi.pingan.fymodule.order.service.ISxoOrderAftersaleService;
import com.ruoyi.pingan.fymodule.order.service.ISxoSafetyOrderRevokeService;
import com.ruoyi.pingan.fymodule.order.service.ISxoSafetyOrderService;
import com.ruoyi.pingan.fymodule.thirdpa.model.dto.ThirdFreezeRechargeCancelDto;
import com.ruoyi.pingan.fymodule.thirdpa.model.dto.TraFinalStatusQueryDto;
import com.ruoyi.pingan.fymodule.thirdpa.model.vo.TraFinalStatusQueryVo;
import com.ruoyi.pingan.fymodule.thirdpa.service.PaOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * 平安分账订单冻结充值撤销 业务层实现
 *
 * @author wangy
 * @since 2025-06-19
 */
@Slf4j
@Service
public class SxoSafetyOrderRevokeServiceImpl extends ServiceImpl<SxoSafetyOrderRevokeMapper, SxoSafetyOrderRevoke> implements ISxoSafetyOrderRevokeService {

    @Autowired
    private PaOrderService paOrderService;

    @Autowired
    private ISxoSafetyOrderService sxoSafetyOrderService;
    @Autowired
    private ISxoOrderAftersaleService sxoOrderAftersaleService;

    @Override
    public void execute6273_6268() {
        log.info("冻结充值撤退状态查询；时间:{}", DateUtil.now());
        Date date = new Date();
        Date startTime = DateUtil.offsetDay(date, -1);
        //Date endTime = DateUtil.offsetMinute(date, -120);
        LambdaQueryWrapper<SxoSafetyOrderRevoke> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SxoSafetyOrderRevoke::getRevokeStatus, 1);
        queryWrapper.eq(SxoSafetyOrderRevoke::getStatus, 0);
        queryWrapper.ge(SxoSafetyOrderRevoke::getRevokeTime, DateUtil.format(startTime, "yyyy-MM-dd").concat(" 00:00:00"));
        queryWrapper.le(SxoSafetyOrderRevoke::getRevokeTime, DateUtil.now());
        queryWrapper.orderByAsc(SxoSafetyOrderRevoke::getId);
        List<SxoSafetyOrderRevoke> lists = this.list(queryWrapper);
        if (lists.isEmpty()) {
            log.info("冻结充值撤退状态查询；时间:{};数量：{}", DateUtil.now(), 0);
            return;
        }
        String dateStr = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
        for (SxoSafetyOrderRevoke sxoSafetyOrderRevoke : lists) {
            if (StrUtil.isEmpty(sxoSafetyOrderRevoke.getJzbAcceptSeqNo())) {
                continue;
            }
            try {
                TraFinalStatusQueryDto dto = new TraFinalStatusQueryDto();
                dto.setOldJzbAcceptSeqNo(sxoSafetyOrderRevoke.getJzbAcceptSeqNo());
                TraFinalStatusQueryVo vo = paOrderService.traFinalStatusQuery(dto);
                if (!"6268".equals(vo.getTranType())) {
                    continue;
                }
                SxoSafetyOrderRevoke mod = new SxoSafetyOrderRevoke();
                mod.setId(sxoSafetyOrderRevoke.getId());
                if ("0".equals(vo.getTranStatus())) {
                    String tranDateStr = vo.getTranDate().concat(vo.getTranTime());
                    Date tranDate = DateUtil.parse(tranDateStr);
                    //撤销状态
                    mod.setRevokeStatus(0);
                    //撤销时间
                    mod.setRevokeSuccTime(DateUtil.format(tranDate, "yyyy-MM-dd HH:mm:ss"));
                    //交易总金额
                    mod.setUpdTime(dateStr);
                    mod.setErrorMsg("");
                    this.updateById(mod);

                    SxoSafetyOrder order = sxoSafetyOrderService.getById(sxoSafetyOrderRevoke.getSafetyOrderId());
                    if (order != null) {
                        SxoSafetyOrder modO = new SxoSafetyOrder();
                        modO.setId(sxoSafetyOrderRevoke.getSafetyOrderId());
                        modO.setUpdTime(DateUtil.now());
                        modO.setRevokeTime(DateUtil.now());
                        modO.setRevokeStatus(1);
                        sxoSafetyOrderService.updateById(modO);
                        javaToPhpOrderRefund(order);
                    }
                }
                if ("1".equals(vo.getTranStatus())) {
                    mod.setRevokeStatus(-1);
                    mod.setErrorMsg(vo.getTranDealStatusDesc());
                    this.updateById(mod);
                }
            } catch (Exception e) {
                log.error("execute6273_6268时间:{};异常：{}", DateUtil.now(), e.getMessage());
            }

        }

        log.info("冻结充值撤退状态查询；时间:{}；数量：{}", DateUtil.now(), lists.size());
    }

    private void javaToPhpOrderRefund(SxoSafetyOrder order){
        SxoOrderAftersale mod = new SxoOrderAftersale();
        mod.setId(order.getSaleId());
        mod.setApplySafeStatus("2");
        sxoOrderAftersaleService.updateById(mod);
        ThirdFreezeRechargeCancelDto dto = new ThirdFreezeRechargeCancelDto();
        dto.setSaleId(order.getSaleId());
        paOrderService.orderRefundToPhp(dto);
    }
}